const STATE_STORAGE_KEY = 'STATE_STORAGE'
const persist = (ctx) => {
    const id = ctx.store.$id
    const currentState = JSON.parse(localStorage.getItem(STATE_STORAGE_KEY+id) || '{}')
    ctx.store.$patch(currentState)

    window.addEventListener('beforeunload', () => {
        const state = ctx.store.$state
        localStorage.setItem(STATE_STORAGE_KEY+id, JSON.stringify(state))
    })
}

export default persist